//
//  ViewController.m
//  DispatchStudy
//
//  Created by alan on 2020/11/24.
//

#import "ViewController.h"
// GCD 文章 https://www.jianshu.com/p/2d57c72016c6
// https://www.jianshu.com/p/0753e51f10af
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 创建一个并发队列
    dispatch_queue_t queue = dispatch_queue_create("upload", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        sleep(3);
        NSLog(@"1");
    });
    dispatch_async(queue, ^{
        sleep(1);
        NSLog(@"2");
    });
    NSLog(@"barrier before");
//    dispatch_barrier_sync(queue, ^{
//        NSLog(@"barrier_sync");
//    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"barrier_sync");
    });
    NSLog(@"barrier after");
    dispatch_async(queue, ^{
        NSLog(@"3");
    });
    NSLog(@"queue end");
}
 // A, B, C任务完成之后(A, B, C无顺序要求), 进行任务D
- (void)multHandleWithBarrier
{
    // 使用dispatch_barrier
    dispatch_queue_t queue = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_CONCURRENT);

    dispatch_async(queue, ^{
        NSLog(@"开始任务A");
        [NSThread sleepForTimeInterval:1];
        NSLog(@"任务A done.");
    });
    dispatch_async(queue, ^{
        NSLog(@"开始任务B");
        [NSThread sleepForTimeInterval:0.5];
        NSLog(@"任务B done.");
    });
    dispatch_async(queue, ^{
        NSLog(@"开始任务C");
        [NSThread sleepForTimeInterval:0.2];
        NSLog(@"任务C done.");
    });

    dispatch_barrier_async(queue, ^{
        NSLog(@"----------> barrier <----------");
    });

    dispatch_async(queue, ^{
        NSLog(@"开始任务D");
    });
}
// A, B, C任务完成之后(A, B, C无顺序要求), 进行任务D
- (void)multHandleWithGroup
{
    dispatch_queue_t queue = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_CONCURRENT);

    dispatch_group_t group = dispatch_group_create();

    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        NSLog(@"开始任务A");
        [NSThread sleepForTimeInterval:3];
        NSLog(@"任务A done.");
        dispatch_group_leave(group);
    });

    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        NSLog(@"开始任务B");
        [NSThread sleepForTimeInterval:2];
        NSLog(@"任务B done.");
        dispatch_group_leave(group);
    });

    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        NSLog(@"开始任务C");
        [NSThread sleepForTimeInterval:1];
        NSLog(@"任务C done.");
        dispatch_group_leave(group);
    });

    dispatch_group_notify(group, queue, ^{
        NSLog(@"开始任务D");
    });
}
// A, B, C任务完成之后(A, B, C顺序要求依次执行), 进行任务D
- (void)multHandleWithOrder
{
    dispatch_queue_t queue = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_CONCURRENT);

    dispatch_barrier_async(queue, ^{
        NSLog(@"开始任务A");
        [NSThread sleepForTimeInterval:1];
        NSLog(@"任务A done.");
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"开始任务B");
        [NSThread sleepForTimeInterval:0.5];
        NSLog(@"任务B done.");
    });
    dispatch_barrier_async(queue, ^{
        NSLog(@"开始任务C");
        [NSThread sleepForTimeInterval:0.2];
        NSLog(@"任务C done.");
    });

    dispatch_barrier_async(queue, ^{
        NSLog(@"开始任务D");
    });
}

@end
